package com.atguigu.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.atguigu.cache.ShopCache;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuImage;
import com.atguigu.entity.SkuInfo;
import com.atguigu.mapper.ProductSalePropertyKeyMapper;
import com.atguigu.mapper.SkuSalePropertyValueMapper;
import com.atguigu.service.SkuDetailService;
import com.atguigu.service.SkuImageService;
import com.atguigu.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SkuDetailServiceImpl implements SkuDetailService {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SkuImageService skuImageService;
    @Autowired
    private ProductSalePropertyKeyMapper productSalePropertyKeyMapper;
    @Autowired
    private SkuSalePropertyValueMapper skuSalePropertyValueMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedissonClient redissonClient;




    private SkuInfo getSkuInfoFromDb(Long skuId) {
        //查询单个sku信息
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        if (skuInfo != null) {
            //查询sku所对应的图片信息
            QueryWrapper<SkuImage> wrapper = new QueryWrapper<>();
            wrapper.eq("sku_id", skuId);
            List<SkuImage> skuImageList = skuImageService.list(wrapper);
            skuInfo.setSkuImageList(skuImageList);
        }
        return skuInfo;
    }

    @Override
    public Map getSkuSalePropertyValueIdMapping(Long productId) {
        Map<Object, Object> retMap = new HashMap<>();
        List<Map> valueIdMap=skuSalePropertyValueMapper.getSkuSalePropertyValueIdMapping(productId);
        if(!CollectionUtils.isEmpty(valueIdMap)){
            for (Map map : valueIdMap) {
                //key是销售属性值id,value是sku的id
                retMap.put(map.get("sale_property_value_id"),map.get("sku_id"));
            }
        }
        return retMap;
    }

    @Override
    public List<ProductSalePropertyKey> getSkuSalePropertyKeyAndValue(long productId, long skuId) {
        return productSalePropertyKeyMapper.getSkuSalePropertyKeyAndValue(productId,skuId);
    }

    //该注解用于添加缓存
    //@Cacheable
    @ShopCache(prefix = "sku:info:")
    @Override
    public SkuInfo getSkuInfo(Long skuId) {
        //查询单个sku信息
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        if(skuInfo!=null){
            //查询sku所对应的图片信息
            QueryWrapper<SkuImage> wrapper = new QueryWrapper<>();
            wrapper.eq("sku_id" ,skuId);
            List<SkuImage> skuImageList = skuImageService.list(wrapper);
            skuInfo.setSkuImageList(skuImageList);
        }
        return skuInfo;
    }
}
